import React, { useEffect, useState } from 'react';
import { Col } from 'antd';
import { AxInput, AxLabel, AxPlaceHolder, AxSelect } from '@/components/AxForm/AxFormItem';
import { axhttp } from '@/shared/axhttp';
import { axhelper } from '@/shared/axhelper';

/** 转专业
 * @param form 表单
 * @param url 请求地址
 * @param setDescription 设置内容的方法
 * @param studentStatusId 学生在校Id
 */
export const Transfer = ({ form, url, setDescription, studentStatusId }: any) => {
    const [schemes, setSchemes] = useState<any>([]);
    const [campus, setCampus] = useState<any>([]);
    const [majors, setMajors] = useState<any>([]);
    const [features, setFeatures] = useState<any>([]);
    const [feeSchemes, setFeeSchemes] = useState<any>([]);
    const [oldData, setOldData] = useState<any>({});
    const { getX } = axhttp;

    useEffect(() => {
        const orgId = JSON.parse(localStorage.getItem('ax-data-app') || '').orgId;
        getX(url.readEnrollSchemeList, { orgId }).then((res: any) => {
            setSchemes(axhelper.transToStandardData(res.data, 'id', 'name'));
        });
    }, []);

    useEffect(() => {
        studentStatusId && getX(url.readStudentInfo, { studentId: studentStatusId }).then((res: any) => {
            setOldData(res.data);
        });
    }, [studentStatusId]);

    const handleChange = (schemeId: any) => {
        if (schemeId) {
            getX(url.readSchemeCampusList, { schemeId }).then((res: any) => {
                setCampus(axhelper.transToStandardData(res.data, 'id', 'name'));
            });
            getX(url.readMajorList, { schemeId }).then((res: any) => {
                setMajors(axhelper.transToStandardData(res.data, 'id', 'name'));
            });
            getX(url.readEnrollFeatureList, { schemeId }).then((res: any) => {
                setFeatures(axhelper.transToStandardData(res.data, 'id', 'name'));
            });
        } else {
            setMajors([]);
            setCampus([]);
            setFeatures([]);
            setFeeSchemes([]);
        }
        getFeeScheme({ schemeId, majorId: undefined, campusId: undefined, featureId: undefined, planId: undefined });
    }

    const getFeeScheme = (obj: any) => {
        const schemeId = obj.hasOwnProperty('schemeId')
            ? obj.schemeId
            : form.getFieldValue('schemeId');
        const majorId = obj.hasOwnProperty('majorId')
            ? obj.majorId
            : form.getFieldValue('majorId');
        const campusId = obj.hasOwnProperty('campusId')
            ? obj.campusId
            : form.getFieldValue('campusId');
        const featureId = obj.hasOwnProperty('featureId')
            ? obj.featureId
            : form.getFieldValue('featureId');
        const planId = obj.hasOwnProperty('planId')
            ? obj.planId
            : form.getFieldValue('planId');

        if (schemeId && majorId && campusId && featureId) {
            const fd0 = schemes.find((f: any) => f.value === schemeId);
            if (fd0) {
                form.setFieldsValue({
                    schemeName: fd0.text,
                });
            }
            const fd1 = majors.find((f: any) => f.value === majorId);
            if (fd1) {
                form.setFieldsValue({
                    majorName: fd1.text,
                });
            }
            const fd2 = campus.find((f: any) => f.value === campusId);
            if (fd2) {
                form.setFieldsValue({
                    campusName: fd2.text,
                });
            }
            const fd3 = features.find((f: any) => f.value === featureId);
            if (fd3) {
                form.setFieldsValue({
                    featureName: fd3.text,
                });
            }
            getX(url.readRepeatFee, { studentId: studentStatusId, schemeId, majorId, campusId, featureId }).then((res: any) => {
                setFeeSchemes(axhelper.transToStandardData(res.data, 'id', 'name'));
            });
        }
        else {
            setFeeSchemes([]);
        }

        handleChangeInfo({ schemeId, majorId, campusId, featureId, planId });
    };

    /** 变更信息 */
    const handleChangeInfo = (obj: any) => {
        const schemeId = obj.hasOwnProperty('schemeId')
            ? obj.schemeId
            : form.getFieldValue('schemeId');
        const majorId = obj.hasOwnProperty('majorId')
            ? obj.majorId
            : form.getFieldValue('majorId');
        const campusId = obj.hasOwnProperty('campusId')
            ? obj.campusId
            : form.getFieldValue('campusId');
        const featureId = obj.hasOwnProperty('featureId')
            ? obj.featureId
            : form.getFieldValue('featureId');
        const planId = obj.hasOwnProperty('planId')
            ? obj.planId
            : form.getFieldValue('planId');

        let changeInfo = '';
        if (schemeId && schemeId !== oldData.schemeId) {
            changeInfo += `原就读方案：${oldData.schemeName}，`;
            changeInfo += `新就读方案：${axhelper.getTextByValue(schemeId, schemes)}；`;
        }
        if (campusId && campusId !== oldData.campusId) {
            changeInfo += `原校区：${oldData.campusName}，`;
            changeInfo += `新校区：${axhelper.getTextByValue(campusId, campus)}；`;
        }
        if (majorId && majorId !== oldData.majorId) {
            changeInfo += `原就读专业：${oldData.majorName}，`;
            changeInfo += `新就读专业：${axhelper.getTextByValue(majorId, majors)}；`;
        }
        if (featureId && featureId !== oldData.featureId) {
            changeInfo += `原特征：${oldData.featureName}，`;
            changeInfo += `新特征：${axhelper.getTextByValue(featureId, features)}；`;
        }
        if (planId && planId !== oldData.planId) {
            changeInfo += `原缴费方案：${oldData.planName}，`;
            changeInfo += `新缴费方案：${axhelper.getTextByValue(planId, feeSchemes)}；`;
        }
        setDescription(changeInfo);
    }

    const handleChangeFeeScheme = (value: any) => {
        const f = feeSchemes.find((f: any) => f.value === value);
        if (f) {
            form.setFieldsValue({
                planName: f.text,
            });
        }
        handleChangeInfo({ planId: value });
    }

    return (
        <>
            <Col span={8}>
                <AxLabel label="姓名" value={oldData.name} />
            </Col>
            <Col span={8}>
                <AxLabel label="身份证" value={oldData.idcard} />
            </Col>
            <Col span={8}>
                <AxPlaceHolder />
            </Col>
            <Col span={8}>
                <AxSelect form={form} label="就读方案" field="schemeId" data={schemes}
                    value={oldData.schemeId} autoSelect onChange={handleChange} required />
            </Col>
            <Col span={8}>
                <AxSelect form={form} label="校区" field="campusId" data={campus}
                    value={oldData.campusId} autoSelect onChange={(v: any) => getFeeScheme({ campusId: v })} required />
            </Col>
            <Col span={8}>
                <AxSelect form={form} label="就读专业" field="majorId" data={majors}
                    value={oldData.majorId} autoSelect onChange={(v: any) => getFeeScheme({ majorId: v })} required />
            </Col>
            <Col span={8}>
                <AxSelect form={form} label="特征" field="featureId" data={features}
                    value={oldData.featureId} autoSelect onChange={(v: any) => getFeeScheme({ featureId: v })} required />
            </Col>
            <Col span={8}>
                <AxSelect form={form} label="缴费方案" field="planId" data={feeSchemes}
                    value={oldData.planId} autoSelect onChange={handleChangeFeeScheme} required />
            </Col>
            <Col span={8}>
                <div style={{ display: 'none' }}>
                    <AxInput form={form} field='name' value={oldData.name} />
                    <AxInput form={form} field='idcard' value={oldData.idcard} />
                    <AxInput form={form} field='schemeName' />
                    <AxInput form={form} field='campusName' />
                    <AxInput form={form} field='majorName' />
                    <AxInput form={form} field='featureName' />
                    <AxInput form={form} field='planName' />
                </div>
            </Col>
        </>
    )
};